﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using RBS.Common;
using RBS.Common.JsonEntity;
using RBS.Core.Extension;
using RBS.Core.Model;
using RBS.Web.Hotel.Models.Order;

namespace RBS.Web.Hotel.Areas.Settlement.Controllers
{
    public class HotelAppController : Controller
    {
        //
        // GET: /Settlement/HotelApp/ 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="hid"></param>
        /// <param name="uid"></param>
        /// <param name="upassword">Md5加密后的密码</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ActionResult CheckOut(int orderid, int hid, string uid, string upassword, string token)
        {
            var tokenTemp = RBS.Common.DEncrypt.DESEncrypt.Encrypt(orderid.ToString() + hid.ToString() + uid.ToString() + upassword.ToString());
            if (tokenTemp != token)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            } 
            else
            {
                var bll = new RBS.Core.BLL.Hotel.H_Hotel_Users();
                var model = bll.UserLogin(uid, upassword);
                if (model != null)
                { 
                    FormsAuthentication.SetAuthCookie(model.H_UID, false);
                    // model.RoleIds = roleType;  
                    Session[RBSKeys.SESSION_HOTEL_INFO] = model;
                    Session.Timeout = 45;
                    //写入Cookies
                    Utils.WriteCookie("DTRememberName", model.H_UserName, 14400);
                    Utils.WriteCookie("AdminName", "DTcms", model.H_UID);
                    Utils.WriteCookie("AdminPwd", "DTcms", model.Password);
                    if (!User.Identity.IsAuthenticated)
                    {
                        FormsAuthentication.SignOut();
                    }
                    FormsAuthentication.SetAuthCookie(model.Id.ToString(), false);
                    return RedirectToAction("jiesuan", "Sm", new { orderid = orderid });
                }
                else
                {
                    return new HttpStatusCodeResult(HttpStatusCode.ResetContent);
                }  
            }
           /* var model = new OrderViewModel
            {
                OrderModel = new RBS.Core.BLL.D_Order().GetModel(orderid),
                OrderDetailsModel = new RBS.Core.BLL.D_Order_Details().GetModelList("OrderId=" + orderid)
            };
            model.SeateName = new RBS.Core.BLL.Hotel.H_Hotel_Seat().GetModelByCache(Convert.ToInt32(model.OrderModel.DineTable)).Name;
            //总金额
            model.OrderModel.OrderAmount = model.OrderDetailsModel.Sum(c => c.Num * c.Price);
            var count = 0;
            //酒店优惠券信息
            DataTable dt = new RBS.Core.BLL.P_Advertis_Payment().GetList(10, 1, " and HotelId=" + model.OrderModel.H_Id + "", " ValidEndTime desc ", out count).Tables[0];
            model.HotelDeed = IListDataSet.DataTableToIList<RBS.Core.Model.P_Advertis_Payment>(dt);
            //食客信息
            if (model.OrderModel.U_Id > 0)
            {
                model.User = new RBS.Core.BLL.U_User().GetModelByCache(model.OrderModel.U_Id);
                model.UserIntegration = new RBS.Core.BLL.U_User_Integration().GetIntegralCount(model.OrderModel.U_Id);
                dt = new RBS.Core.BLL.P_Advertis_Payment().GetListByUser(10, 1, "and U_Id= " + model.User.Id + " and HotelId=" + model.OrderModel.H_Id + "  and ValidEndTime>=GETDATE()", " ValidEndTime desc ", out count).Tables[0];
                IListDataSet.DataTableToIList<RBS.Core.Model.P_Advertis_Payment>(dt);
                model.UserDeed = IListDataSet.DataTableToIList<RBS.Core.Model.P_Advertis_Payment>(dt);
            }
            model.Seetlement = new D_Settlement();
            return View(model);*/
        }
         
       /* [HttpPost]
        public ActionResult CheckOut(OrderViewModel orderViewModel, FormCollection collection)
        {
          /*  var orderBll = new RBS.Core.BLL.D_Order();
            var a = orderViewModel;
            var order = orderBll.GetModel(a.OrderModel.Id);

            //添加结帐单
            var settlement = new RBS.Core.Model.D_Settlement
            {
                HotelId = order.H_Id,
                OrderId = order.Id,
                UserId = order.U_Id,
                OrderNo = orderViewModel.Number,
                PayType = orderViewModel.Seetlement.PayType,
                PayTime = DateTime.Now,
                RecAmount = orderViewModel.Seetlement.RecAmount,
                GiveAmount = orderViewModel.Seetlement.GiveAmount,
                OrderAmount = orderViewModel.Seetlement.OrderAmount,
                Amount = orderViewModel.Seetlement.Amount,
                DisAmount = orderViewModel.Seetlement.DisAmount,
                PaidAmount = orderViewModel.Seetlement.PaidAmount,
                JF = orderViewModel.Seetlement.JF,
                /*
                                SendFoodId = orderViewModel.Seetlement.JF,
                                SendFood = orderViewModel.Seetlement.JF,
                                ConsumptionId = orderViewModel.Seetlement.JF,
                                Consumption =  orderViewModel.Seetlement.JF,
                                AdvertisId = orderViewModel.Seetlement.JF,
                                Advertis = orderViewModel.Seetlement.JF,#2#
            };
            new RBS.Core.BLL.D_Settlement().Add(settlement);


            //修改订单


            //积分，优惠券结算

            //积分，优惠券赠送 
            if (collection["HotelDeedCKB"] != null && order.U_Id > 0)
            {
                var hdeed = collection["HotelDeedCKB"].Split(',');
                foreach (var s in hdeed)
                {
                    JsonData jsonData = ApiRepository.Get("api/Deed/HotelDeed?uid=" + order.U_Id + "&apid=" + s + "&num=1");
                }
            }#1#
            return Content(order.Id.ToString());
             
        }*/

    }
}
